package com.aneliya.dao;

import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.aneliya.model.User;

public class UserDAO implements IUserDAO {
	
	private HibernateTemplate hibernateTemplate;
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public User getUserByUsername(final String username) {
		HibernateCallback callback = new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				User user = (User) session
					.createQuery("from User as user where user.username = ?")
					.setString(0, username)
					.uniqueResult();
				return user;
			}
		};
		User user = (User) hibernateTemplate.execute(callback);
		return user;
	}

	public void saveUser(final User user) {
		HibernateCallback callback = new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				session.save(user);
				return null;
			}
		};
		hibernateTemplate.execute(callback);
	}

}
